package com.ruoyi.student.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.vo.TGradeVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.student.domain.TGrade;
import com.ruoyi.student.service.ITGradeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 年级Controller
 * 
 * @author ruoyi
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/student/grade")
public class TGradeController extends BaseController
{
    @Autowired
    private ITGradeService tGradeService;

    /**
     * 查询年级列表
     */
    @PreAuthorize("@ss.hasPermi('student:grade:list')")
    @GetMapping("/list")
    public TableDataInfo list(TGrade tGrade)
    {
        startPage();
//        List<TGrade> list = tGradeService.selectTGradeList(tGrade);
        List<TGradeVo> tGradeVos = tGradeService.selectTGradeVoList(tGrade);
        return getDataTable(tGradeVos);
    }

    /**
     * 导出年级列表
     */
    @PreAuthorize("@ss.hasPermi('student:grade:export')")
    @Log(title = "年级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TGrade tGrade)
    {
        List<TGrade> list = tGradeService.selectTGradeList(tGrade);
        ExcelUtil<TGrade> util = new ExcelUtil<TGrade>(TGrade.class);
        util.exportExcel(response, list, "年级数据");
    }

    /**
     * 获取年级详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:grade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tGradeService.selectTGradeById(id));
    }

    /**
     * 新增年级
     */
    @PreAuthorize("@ss.hasPermi('student:grade:add')")
    @Log(title = "年级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGrade tGrade)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tGrade.setCreatorId(user.getUserId());
        return toAjax(tGradeService.insertTGrade(tGrade));
    }

    /**
     * 修改年级
     */
    @PreAuthorize("@ss.hasPermi('student:grade:edit')")
    @Log(title = "年级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGrade tGrade)
    {
        return toAjax(tGradeService.updateTGrade(tGrade));
    }

    /**
     * 删除年级
     */
    @PreAuthorize("@ss.hasPermi('student:grade:remove')")
    @Log(title = "年级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tGradeService.deleteTGradeByIds(ids));
    }
}
